﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using BLL.BaseDictionaryService;
using BLL.PayRecordsTransaction;
using Model;

namespace FamilyManager
{
    public partial class PayRecordEditForm : Form
    {
        public PayRecordEditForm(int _RecordOID)
        {
            InitializeComponent();
            this.recordOID = _RecordOID;
        }

        private int recordOID;


        private void BindcbUsers()
        {
            cbUsers.Items.Insert(0, "");
            QueryUsersTransaction command = new QueryUsersTransaction();
            command.Execute();
            cbUsers.DataSource = command.Users;
            cbUsers.DisplayMember = "Db_NickName";
            cbUsers.ValueMember = "Db_OID";
        }

        private void BindcbPayNecessity()
        {
            QueryPayNecessityTransaction command = new QueryPayNecessityTransaction();
            command.Execute();
            cbPayNecessity.DataSource = command.PayNecessitys;
            cbPayNecessity.DisplayMember = "Db_PayNecessity";
            cbPayNecessity.ValueMember = "Db_OID";
        }

        private void BindcbPayCategory()
        {
            QueryPayCategoryTransaction command = new QueryPayCategoryTransaction();
            command.Execute();
            this.cbPayCategory.DataSource = command.PayCategorys;
            this.cbPayCategory.ValueMember = "Db_OID";
            this.cbPayCategory.DisplayMember = "Db_CategoryName";
        }

        private void PayRecordEditForm_Load(object sender, EventArgs e)
        {
            BindcbUsers();
            BindcbPayCategory();
            BindcbPayNecessity();
            InitData();
        }

        private void InitData()
        {
            LoadSinglePayRecordsTransaction objLoadSinglePayRecordsTransaction = new LoadSinglePayRecordsTransaction(recordOID);
            T_PayRecordsObject ResultRecord = objLoadSinglePayRecordsTransaction.ResultRecord;
            dtpPayDate.Value = ResultRecord.Db_PayDate;
            cbPayNecessity.SelectedValue = ResultRecord.Db_PayNecessity;
            txtPayItem.Text = ResultRecord.Db_PayItem;
            cbPayCategory.SelectedValue = ResultRecord.Db_PayCategoryOID;
            txtPaySum.Text = ResultRecord.Db_PaySum.ToString();
            cbUsers.SelectedValue = ResultRecord.Db_PayObject;
            txtPayMemo.Text = ResultRecord.Db_PayMemo;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            T_PayRecordsObject objPayRecord = new T_PayRecordsObject();
            objPayRecord.Db_PayDate = DateTime.Parse(DateTime.Parse(dtpPayDate.Text).ToShortDateString());
            objPayRecord.Db_PayItem = txtPayItem.Text;
            objPayRecord.Db_PayNecessity = int.Parse(cbPayNecessity.SelectedValue.ToString());
            objPayRecord.Db_PayCategoryOID = int.Parse(cbPayCategory.SelectedValue.ToString());
            objPayRecord.Db_PayObject = int.Parse(cbUsers.SelectedValue.ToString());
            objPayRecord.Db_PaySum = decimal.Parse(txtPaySum.Text);
            objPayRecord.Db_PayMemo = txtPayMemo.Text;
            objPayRecord.Db_OID = recordOID;
            UpdatePayRecordsTransaction objUpdatePayRecordsTransaction = new UpdatePayRecordsTransaction(objPayRecord);
            objUpdatePayRecordsTransaction.Execute();
            MessageBox.Show("修改成功", "操作提示");
            this.DialogResult = DialogResult.Yes;
            this.Close();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("确认删除当前记录?", "提示", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                LoadSinglePayRecordsTransaction objLoadSinglePayRecordsTransaction = new LoadSinglePayRecordsTransaction(recordOID);
                T_PayRecordsObject ResultRecord = objLoadSinglePayRecordsTransaction.ResultRecord;

                DeletePayRecordsTransaction objDeletePayRecordsTransaction = new DeletePayRecordsTransaction(recordOID);
                objDeletePayRecordsTransaction.Execute();
                if (objDeletePayRecordsTransaction.Result == true)
                {
                    MessageBox.Show("删除成功!已删除信息：消费项目:" + ResultRecord.Db_PayItem + "   消费金额:" + ResultRecord.Db_PaySum + "   消费人:" + ResultRecord.UserNickName);
                    this.DialogResult = DialogResult.Yes;
                    Close();
                }
                else
                {
                    MessageBox.Show("删除失败!");
                }
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
